<?php
namespace Home\Controller;

use Common\Util\RSA;
/**
 * 邮箱验证
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class MailController extends HomeController {

    public $request;

    public function _initialize()
    {
        parent::_initialize();
        if (I('_sign_type') == 'rsa' && I('sign')){
            $RSA = new RSA(C('RSA_PUBLIC_KEY'), C('RSA_PRIVATE_KEY'));
            $this->request = $RSA->privateDecrypt(I('sign'))->toJson();
        }
    }

    public function verify()
    {
        if(!$this->request || $this->request['expired'] < NOW_TIME){
            // exit('邮件验证失败');
            redirect(U('/'), 5, '邮件已失效，请重新验证！5秒后跳转...');
        }
        $user = service('users')->where(['id' => intval($this->request['uid'])])->find();

        if ($user) {
            // 检查邮件token是否验证过
            $send_log = service('UserEmailSendLog')->where(['_id' => new \MongoId($this->request['_id'])])->find();

            if (empty($send_log) || $send_log['status'] == 1) {
                redirect(U('/'), 5, '邮件已失效，请重新验证！5秒后跳转...');
            }

            // 修改用户信息
            service('users')->save([
                'id' => intval($this->request['uid']),
                'email' => $this->request['email'],
                'email_verify' => 1
            ]);
            // 修改邮件token使用状态
            service('UserEmailSendLog')->save([
                '_id' => new \MongoId($this->request['_id']),
                'status' => 1
            ]);

            // 用户邮箱绑定送6币
            $result['score'] = event('Score')->get($this->request['uid'], 'mail_verify');
            // exit('邮件验证成功');
            redirect(U('/'), 5, '邮件验证成功！5秒后跳转...');
        }
        // exit('邮件验证失败');
        redirect(U('/'), 5, '邮件验证失败！5秒后跳转...');
    }

    /**
     * 邮件重置支付密码
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function findPassword()
    {
        if (IS_POST) {
            $uid = session('uid');
            $mail_log_id = session('mail_log_id');
            $pwd = I('pwd', '', 'trim');
            $repwd = I('repwd', '', 'trim');
            if (!$uid || !$mail_log_id || $pwd != $repwd) {
                exit('用户信息有误或两次密码不一致');
            }

            $params = [
                'uid' => $uid,
                'password' => $pwd,
            ];
            $response = event('LocalClient')->doRequest('password', $params);
            if ($response['code'] != 200) {
                exit($response['info']);
            }

            // 修改邮件token使用状态
            service('UserEmailSendLog')->save([
                '_id' => new \MongoId($mail_log_id),
                'status' => 1
            ]);

            // 删除session
            session('uid', null);
            session('mail_log_id', null);
            exit('支付密码修改成功');
        }
        if(!$this->request || $this->request['expired'] < NOW_TIME){
            redirect(U('/'), 5, '邮件已失效，请重新验证！5秒后跳转...');
        }
        $user = service('users')->where(['id' => intval($this->request['uid'])])->find();

        // 检查邮件token是否验证过
        $send_log = service('UserEmailSendLog')->where(['_id' => new \MongoId($this->request['_id'])])->find();
        if (empty($send_log) || $send_log['status'] == 1) {
            redirect(U('/'), 5, '邮件已失效，请重新验证！5秒后跳转...');
        }

        if ($user) {
            session('uid', $user['id']);
            session('mail_log_id', $this->request['_id']);
            C('DEFAULT_THEME', 'v4');
            return $this->display();
        }
        // exit('邮件验证失败');
        redirect(U('/'), 5, '邮件验证失败，请重新验证！5秒后跳转...');
    }

}